//
//  ZMyOrderListVC.m
//  DistributorAB
//
//  Created by LZY on 2019/10/14.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "ZMyOrderListVC.h"
#import "ZMyOrderListShopInfoCell.h"//订单店铺信息cell
#import "ZMyOrderListOrderInfoCell.h"//订单产品cell

#import "ZMyOrderListOrderModel.h"

#import "BBuyNowShopStoreVC.h"//店铺详情

#import "ZMyOrderListOrderChangeView.h"//订单修改数量弹窗
#import "ZMyOrderListOrderCancelView.h"//订单取消弹窗

#import "ZMyOrderDetailVC.h"//订单详情

@interface ZMyOrderListVC () <UITableViewDataSource,UITableViewDelegate,ZMyOrderListShopInfoCellDelegate,ZMyOrderListOrderInfoCellDelegate,ZMyOrderListOrderCancelViewDelegate,ZMyOrderListOrderChangeViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *orderListArray;
@property (nonatomic, strong) NSIndexPath *indexPathSelected;//被操作的indexPath
@end

@implementation ZMyOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"预订单";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    
    _orderListArray = [NSMutableArray array];
    
    [self setupViewAction];
    
    [self requestData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:USER_MODIFY_ORDER_SUCCESS object:nil];
}
#pragma mark - 界面布局
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNaviHeight, kScreen_Width, kScreen_Height - kNaviHeight) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    KKTableViewNoDataView *viewPlaceHolder = [[KKTableViewNoDataView alloc] initWithFrame:CGRectMake((kScreen_Width - 100) / 2.0, (kScreen_Height - 72) / 2.0, 100, 72) image:imgname(@"tableviewPlaseholder") viewClick:^{}];
    viewPlaceHolder.backgroundColor = kColorWithHex(0xffffff);
    _tableView.placeHolderView = viewPlaceHolder;
    
    [_tableView registerClass:[ZMyOrderListShopInfoCell class] forCellReuseIdentifier:NSStringFromClass([ZMyOrderListShopInfoCell class])];
    [_tableView registerClass:[ZMyOrderListOrderInfoCell class] forCellReuseIdentifier:NSStringFromClass([ZMyOrderListOrderInfoCell class])];
    
}
#pragma mark - 数据请求
- (void)requestData{
    kWeakSelf; 
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObjectIfNotNil:@"1" forKey:@"user_type"];
    [NetWorkTool POST:MY_PARTS_ORDERS param:dict success:^(id dic) {
        [weakSelf.orderListArray removeAllObjects];
        NSArray *orderList = [dic objectForKeyNotNil:@"data"];
        [orderList enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ZMyOrderListOrderModel *model = [ZMyOrderListOrderModel mj_objectWithKeyValues:obj];
            [weakSelf.orderListArray addObjectNotNil:model];
        }];
        [weakSelf.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.orderListArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    ZMyOrderListOrderModel *model = [self.orderListArray objectAtIndexSafe:section];
    return model.parts.count + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZMyOrderListOrderModel *model = [self.orderListArray objectAtIndexSafe:indexPath.section];
    if (indexPath.row == 0) {
        ZMyOrderListShopInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZMyOrderListShopInfoCell class]) forIndexPath:indexPath];
        cell.orderModel = model;
        cell.delegate = self;
        return cell;
    }else{
        BShopPartModel *partModel = [model.parts objectAtIndexSafe:indexPath.row - 1];
        ZMyOrderListOrderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZMyOrderListOrderInfoCell class]) forIndexPath:indexPath];
        cell.partModel = partModel;
        cell.delegate = self;
        cell.indexPath = indexPath;
        return cell;
    }
    return [UITableViewCell new];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return [ZMyOrderListShopInfoCell cellHeight];
    }else{
        return [ZMyOrderListOrderInfoCell cellHeight];
    }
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZMyOrderListOrderModel *model = [self.orderListArray objectAtIndexSafe:indexPath.section];
    ZMyOrderDetailVC *vc = [ZMyOrderDetailVC new];
    vc.orderID = model.Id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - ZMyOrderListShopInfoCellDelegate
- (void)shopInfoCellClickWith:(ZMyOrderListOrderModel *)model{
//    BBuyNowShopStoreVC *vc = [BBuyNowShopStoreVC new];
//    vc.storeID = model.factoryId;
//    vc.orderListModel = model;
//    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - ZMyOrderListOrderInfoCellDelegate
- (void)orderListOrderInfoCellBtnChangeClick:(NSIndexPath *)indexPath{
    self.indexPathSelected = indexPath;
    ZMyOrderListOrderChangeView *viewChange = [[ZMyOrderListOrderChangeView alloc] init];
    viewChange.delegate = self;
    ZMyOrderListOrderModel *model = [self.orderListArray objectAtIndexSafe:indexPath.section];
    BShopPartModel *orderModel = [model.parts objectAtIndexSafe:indexPath.row - 1];
    viewChange.orderModel = orderModel;
    [viewChange showView];
}
- (void)orderListOrderInfoCellBtnCancelClick:(NSIndexPath *)indexPath{
    self.indexPathSelected = indexPath;
    ZMyOrderListOrderCancelView *viewCancel = [[ZMyOrderListOrderCancelView alloc] init];
    viewCancel.delegate = self;
    ZMyOrderListOrderModel *model = [self.orderListArray objectAtIndexSafe:indexPath.section];
    BShopPartModel *orderModel = [model.parts objectAtIndexSafe:indexPath.row - 1];
    viewCancel.orderModel = orderModel;
    [viewCancel showView];
}
#pragma mark - ZMyOrderListOrderCancelViewDelegate
- (void)orderCancelSuccess{
    //刷新section
    //NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:self.indexPathSelected.section];
    //[_tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
    
    //刷新cell
    //[_tableView reloadRowsAtIndexPaths:@[self.indexPathSelected] withRowAnimation:UITableViewRowAnimationNone];
   
    //因为要删除取消的内容所以选择
    [self requestData];
}
#pragma mark - ZMyOrderListOrderChangeViewDelegate
- (void)orderUpdateSuccess{
    //刷新cell
    [_tableView reloadRowsAtIndexPaths:@[self.indexPathSelected] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
